import { statusOptions } from '@/views/vehicle-monitor/options'
import { VehicleStatusEnum } from '@/views/vehicle-monitor/enums'
import type { VehicleTrackData } from '@/api/vehicle-track/vehicleTrack'

export function getPolylineGroups(aMap, lineData: VehicleTrackData[], leftSpeed: number, rightSpeed: number) {
  if (lineData.length !== 0) {
    const blueArr: VehicleTrackData[] = []
    const greenArr: VehicleTrackData[] = []
    const redArr: VehicleTrackData[] = []
    const vehicleConfig = {
      showDir: true, // 显示方向
      strokeWeight: 6, // 线宽
      strokeOpacity: 1,
      lineCap: 'round'
    }

    for (let i = 0, len = lineData.length; i < len; i++) {
      lineData[i].speedIndex = i

      const speed = +lineData[i].speed
      if (speed < leftSpeed) {
        blueArr.push(lineData[i])
      } else if (speed >= leftSpeed && speed < rightSpeed) {
        greenArr.push(lineData[i])
      } else {
        redArr.push(lineData[i])
      }
    }

    const bluePoylineArr = pointGroup(aMap, lineData, blueArr)
    const greenPoylineArr = pointGroup(aMap, lineData, greenArr)
    const redPoylineArr = pointGroup(aMap, lineData, redArr)
    const polyLineArr = []

    for (let i = 0; i < bluePoylineArr.length; i++) {
      const bluePolyLine = new aMap.Polyline({ path: bluePoylineArr[i], strokeColor: '#0066ff', ...vehicleConfig })
      polyLineArr.push(bluePolyLine)
    }

    for (let i = 0; i < greenPoylineArr.length; i++) {
      const greenPolyLine = new aMap.Polyline({ path: greenPoylineArr[i], strokeColor: '#00ff00', ...vehicleConfig })
      polyLineArr.push(greenPolyLine)
    }

    for (let i = 0; i < redPoylineArr.length; i++) {
      const redPolyLine = new aMap.Polyline({ path: redPoylineArr[i], strokeColor: '#ff3300', ...vehicleConfig })
      polyLineArr.push(redPolyLine)
    }

    return new aMap.OverlayGroup(polyLineArr)
  }
}

export async function getMarkerGroups(aMap, lineData: VehicleTrackData[]) {
  const { vehicleMarker, startMarker, endMarker } = await createMarkers(aMap, lineData[0], lineData[lineData.length - 1])
  const markerGroup = new aMap.OverlayGroup([vehicleMarker, startMarker, endMarker])
  return {
    markerGroup,
    vehicleMarker
  }
}

export function getMoveLineArr(lineData: VehicleTrackData[]) {
  const path = []
  for (let i = 0, len = lineData.length; i < len; i++) {
    path.push([lineData[i].lng, lineData[i].lat])
  }
  return path
}

export function getAssetsFile(url: string) {
  return new URL(`../../assets/images/${url}`, import.meta.url).href
}

async function createMarkers(aMap, startPoint: VehicleTrackData, endPoint: VehicleTrackData) {
  const vehicleColor = statusOptions.find(option => option.value === VehicleStatusEnum.InTransit)?.color
  //  创建汽车marker
  const vehicleMarker = new aMap.Marker({
    position: [startPoint.lng, startPoint.lat],
    angle: startPoint.course ?? 0,
    content: `<svg  width="26" height="26" fill="${vehicleColor}" ><use xlink:href="#vehiclePosition"></use></svg>`,
    offset: new aMap.Pixel(-16, -16)
  })

  // 创建起始点和终点图标
  const startIcon = new aMap.Icon({
    size: new aMap.Size(32, 32),
    image: getAssetsFile('map/start.png'),
    imageSize: new aMap.Size(32, 32),
    imageOffset: new aMap.Pixel(0, 0)
  })

  const endIcon = new aMap.Icon({
    size: new aMap.Size(32, 32),
    image: getAssetsFile('map/end.png'),
    imageSize: new aMap.Size(32, 32),
    imageOffset: new aMap.Pixel(0, 0)
  })

  // 创建起始点和终点marker
  const startMarker = new aMap.Marker({
    position: new aMap.LngLat(startPoint.lng, startPoint.lat),
    icon: startIcon,
    offset: new aMap.Pixel(-13, -30)
  })

  const endMarker = new aMap.Marker({
    position: new aMap.LngLat(endPoint.lng, endPoint.lat),
    icon: endIcon,
    offset: new aMap.Pixel(-13, -30)
  })

  return { vehicleMarker, startMarker, endMarker }
}

// 坐标点分组
function pointGroup(aMap, lineData: VehicleTrackData[], arr: VehicleTrackData[]) {
  const polylineGroup = []
  let index = 0
  for (let i = 0; i < arr.length; i++) {
    const point = new aMap.LngLat(arr[i].lng, arr[i].lat)
    point.speedIndex = arr[i].speedIndex
    if (!polylineGroup[index]) polylineGroup[index] = []
    if (i === 0 || arr[i].speedIndex - arr[i - 1].speedIndex === 1) {
      // 如果当前项是第一项，或者是上一项的下一个相邻点就直接push进去
      polylineGroup[index].push(point)
    } else if (arr[i].speedIndex - arr[i - 1].speedIndex > 1) {
      // 如果当前项不是上一项的相邻点，则在上一项后面补一个相邻点，在开启一个新数组push当前项
      const supplyPoint = lineData[arr[i - 1].speedIndex + 1]
      const _point = new aMap.LngLat(supplyPoint.lng, supplyPoint.lat)
      _point.speedIndex = supplyPoint.speedIndex
      polylineGroup[index].push(_point)
      if (i <= arr.length - 1) {
        index += 1
        polylineGroup[index] = []
        polylineGroup[index].push(point)
      }
    }
    if (i === arr.length - 1) {
      // 最后一项始终在后面补上相邻点
      if (arr[i].speedIndex + 1 < lineData.length) {
        const supplyPoint = lineData[arr[i].speedIndex + 1]
        const _point = new aMap.LngLat(supplyPoint.lng, supplyPoint.lat)
        _point.speedIndex = supplyPoint.speedIndex
        polylineGroup[index].push(_point)
      }
    }
  }
  return polylineGroup
}
